<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("MerchantClass.php");						//--> Merchant
include_once("Operation.php");						//--> Operation
include_once("UploadFiles.php");					//--> UploadFiles
include_once("Files.php");							//--> Files
require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation();						//--> Operation
$objMerchant	= new MerchantClass(DB_TAG_COUPON, $uid);	//--> Merchant

$objFiles		= new Files(DB_TAG_COUPON, $uid);		//--> Files
$objUploadFiles	= new UploadFiles(DB_TAG_COUPON, $uid);	//--> UploadFiles

$error_message 	= '';


$arrAllImage 		= array();
$arrExistedImage 	= array();

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);

$imageid 	= $_GET['imageid'];

if(isset($_GET['action']))
	$action 	= $_GET['action'];
	
if(isset($_POST['action']))
	$action 	= $_POST['action'];
	
if(isset($_GET['backurl_base64']))
	$backurl = base64_decode($_GET['backurl_base64']);
	

if($action == 'delete'){
	$arrDBImage = $objUploadFiles->getByID($arrOperate, $imageid);
	
	$imageid 	= $arrDBImage['id'];
	$imageName	= $arrDBImage['extName'];
	
	$path = $objFiles->getFullPathByID(IMAGE_PATH_LOGO, $imageid, $imageid . '.' . $imageName);
	
	if (file_exists($path)) {
		unlink($path);
		
	}
	
	$objUploadFiles->delete($arrOperate, $imageid);
	$objMerchant->deleteImage($arrOperate, $id, $imageid);
	
}

if($action == 'setMain'){
	$level = $_GET['ismain'];
	
	$objMerchant->update(DB_TAG_COUPON, "UPDATE merchant_images SET level = '$level' WHERE id = " . $_GET['piid']);
}

if($action == 'Upload'){
	$arrImages = array();

	$blacklist = array(".php", ".phtml", ".php3", ".php4");
	foreach ($_FILES["image"]["error"] as $key => $error) {  
    	if ($error == UPLOAD_ERR_OK) { 	
			foreach ($blacklist as $item) {
				if(preg_match("/$item\$/i", $_FILES['image']['name'][$key])) {
				 	$error_message = "only support gif and jpg format.";
				}
			}
			
			if(empty($error_message)){
				$imageinfo = getimagesize($_FILES['image']['tmp_name'][$key]); 
	
				if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') { 
					$error_message = "only support gif and jpg format.";
				}else if($file_size >1024000)
					$error_message = 'Your image more than 1024 kb('.ceil($file_size/1024).'kb)';
				else{
					/*-- get ext image file name --*/
					$arr_image_extend_name 	= explode('/', $imageinfo['mime']);
					$image_extend_name 		= $arr_image_extend_name[count($arr_image_extend_name) - 1];
					
					$arrImage = array();
					$arrImage['name'] 		= $_FILES['image']['name'][$key];
					$arrImage['tmp_name'] 	= $_FILES['image']['tmp_name'][$key];
					$arrImage['file_size']	= filesize($_FILES['image']['tmp_name'][$key]);
					
					$arrImage['width']		= $imageinfo[0];
					$arrImage['height']		= $imageinfo[1];
					
					$arrImage['md5']		= md5(file_get_contents($_FILES['image']['tmp_name'][$key]));
					$arrImage['extName']	= $image_extend_name;
					
					$arrImages[] 			= $arrImage;
				}
			}		
    	}
    	
    	if(!empty($error_message)){
    		break;
    	}
	}
	
	foreach ($arrImages as $arrImg) {
		$imageid = $objUploadFiles->isExisted($arrOperate, UploadFiles::TYPE_IMAGE, $arrImg['md5']);
		if(!empty($imageid)){
			$arrImageInfo = $objUploadFiles->getByID($arrOperate, $imageid);
			
			$arrExistedImage[$imageid] 	= $arrImageInfo['extName'];
			
			$merchantIDs = $objMerchant->merchantIDsOfImage($arrOperate, $imageid);
			
			$error_message .= ' Image ' . $arrImg['name'] . ' existed. Merchant ID list : ' . $merchantIDs; 
			
			continue;
		}
		
		$imageid = $objUploadFiles->add($arrOperate, $arrImg['extName'], $arrImg['md5'], $arrImg['width'], $arrImg['height'], $arrImg['file_size']);
		
		if(!empty($imageid)){
			$objMerchant->addImage($arrOperate, $id, $imageid);
			
			$imageName = $imageid . '.' . strtolower($arrImg['extName']);
			
			$target = $objFiles->getFullPathByID(IMAGE_PATH_LOGO, $imageid, $imageName);
			
			echo 'target = ' . $target;
			if (!move_uploaded_file($arrImg['tmp_name'], $target)) {//-- save image
				$error_message .= "upload image failure.";
			}else{
				$error_message .= "upload image success.";
			}
		}else{
			$error_message .= 'write upload_files failure.';
		}
	}
}

if(!empty($id)){
	$sql 		= "SELECT p.id, p.imageID, p.level, u.extName FROM merchant_images p, upload_files u WHERE p.imageID = u.id AND p.merchantID = $id";
	$arrImages 	= $objMerchant->getAllData(DB_TAG_COUPON, $sql);
	
}

$sql		= "SELECT * FROM merchant WHERE id = $id";
$merInfo 	= $objMerchant->getRow(DB_TAG_COUPON, $sql);

/*----- out html -----*/
$smarty = new Smarty();	//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id',$id);

$smarty->assign('backurl',$backurl);
$smarty->assign('backurl_base64',base64_encode($backurl));


$smarty->assign('arrImages', $arrImages);

$smarty->assign('merInfo', $merInfo);



$smarty->display('listMerchant_Logo.htm');
?>
